package com.zygame.sdk.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.zygame.sdk.ConstantValue;
import com.zygame.sdk.ZyGameSDK;
import com.zygame.sdk.model.UserModel;
import com.zygame.sdk.utils.LoginUtil;
import com.zygame.sdk.utils.SharedPreferencesHelper;
import com.zygame.sdk.utils.http.HttpUtil;
import com.zygame.sdk.utils.http.NetHttpUtil;
import com.zygame.sdk.utils.log.LogHelper;
import com.zygame.sdk.utils.toast.ToastUtil;
import com.zygame.sdk.utils.util.IabHelper;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author duguodong
 * @time 2019/3/28
 * @des ${TODO}
 */
public class ZY_BaseActivity extends FragmentActivity {

    protected IabHelper mHelper;
    private static final int RC_SIGN_IN = 10001;
    private JSONObject jsonObject;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mHelper = new IabHelper(this, ConstantValue.ZY_BASE64KEY);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (mHelper == null)
            return;

        // Pass on the activity result to the helper for handling
        if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
            // not handled, so handle it ourselves (here's where you'd
            // perform any handling of activity results not related to in-app
            // billing...
            super.onActivityResult(requestCode, resultCode, data);
        } else {
            Log.d("GD", "onActivityResult handled by IABUtil.");
        }

        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            Log.i("gd", "status: " + result.getStatus().toString()  + "  code: " + result.getStatus().getStatusCode());
            if (result.isSuccess()) {
                // Google Sign In was successful, authenticate with Firebase
                ToastUtil.showToast(this,"Google登录成功");
                GoogleSignInAccount account = result.getSignInAccount();

                if(account == null){
                    ToastUtil.showToast(this,"获取信息失败,请重试");
                    return;
                }


                String zy_gmail = SharedPreferencesHelper.getInstance().getZY_GMAIL(this);
                String token = SharedPreferencesHelper.getInstance().getToken(this);

                jsonObject = null;
                try {
                    jsonObject = new JSONObject();
                    jsonObject.put("userID", account.getId());
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                HttpUtil.login("", "", account.getServerAuthCode(), "5", jsonObject.toString(), this, onLoginCallback);
                //firebaseAuthWithGoogle(account);
            } else {
                ToastUtil.showToast(this, "Google登录失败 code: " + result.getStatus().getStatusCode());
                // Google Sign In failed, update UI appropriately
                // ...
            }
        }
    }




    NetHttpUtil.DataCallback<JSONObject> onLoginCallback = new NetHttpUtil.DataCallback<JSONObject>() {

        @Override
        public void callbackSuccess(JSONObject paramObject) {
            LogHelper.i("login接口", paramObject.toString());
            String code = paramObject.optString("code");
            //et_account.hidePromptText();

            if (code.equals("0")) {
                String user_name = paramObject.optString("user_name");
                LoginUtil.loginResult(user_name, user_name, paramObject, ZY_BaseActivity.this,"5",1,jsonObject.toString());
                //mContext.getSharedPreferences("tokeninfo", 0).edit().putString("login_token",paramObject.optString("login_token")).commit();
                ZyGameSDK.getInstance().onLoginResult(UserModel.getInstance());
            } else {
                ToastUtil.showToast(ZY_BaseActivity.this,paramObject.optString("message"));
            }
        }

        @Override
        public void callbackError(String error) {
            LogHelper.i("login接口", error);
            //ZyGameSDK.getInstance().onResult(CmCode.CODE_LOGIN_FAIL, "65 sdk login fail !");
        }
    };
}
